/**
 * 
 */
package pers.kevin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;

import pers.kevin.bean.ConfigBean;


/**
 * @author hccb
 *
 */
public class ConfigReader {
	
	public static ConfigBean readConfig(String reqUri, ConfigBean bean) 
	{
		ClassLoader cl = ConfigReader.class.getClassLoader();
		String line;
		try {
			String filepath = cl.getResource("TransConfig.conf").toURI().getPath();
			File file = new File(filepath);
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String []array;
			while((line = reader.readLine()) != null)
			{
				if(line.equals("") || line == null || line.substring(0,1).equals("#"))
					continue;
				array = line.split("&");
				if(reqUri.equals(array[0].trim().toString()))
				{
					for(int i=0; i<array.length; i++)
					{
						if(i==0)
							bean.setReqUri(array[i].trim().toString());
						else if(i==1)
							bean.setMangerClass(array[i].trim().toString());
						else if(i==2)
							bean.setHostIp(array[i].trim().toString());
						else if(i==3){
							if( array[i].trim() == null || array[i].trim().equals("") )
								bean.setHostPort(0); 
							else
								bean.setHostPort(Integer.parseInt(array[i].trim()));
						}
						else if(i==4){
							if( array[i].trim() == null || array[i].trim().equals("") )
								bean.setTimeOut(30000); 
							else
								bean.setTimeOut(Integer.parseInt(array[i].trim()));
						}
					}
				}
				else
					continue;
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return bean;
	}
}
